package com.stepforward.me;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class AlarmHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 2;
	private static final String dbName = "db2";
	private static final String MYFAVORITES_TABLE_NAME = "alarm";
	private static final String KEY_TIME = "time";
	private static final String KEY_DEPARTURE = "departure";
	private static final String KEY_ARRIVAL = "arrival";
	private static final String KEY_ALARMTIME = "alarmtime";
	private static final String KEY_ALARMFREQ = "alarmfrequency";
	private static final String KEY_ID = "fid";
	private static final String MYFAVORITES_TABLE_CREATE = "CREATE TABLE "
			+ MYFAVORITES_TABLE_NAME + " (" + KEY_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_TIME + " TEXT,"
			+ KEY_DEPARTURE + " TEXT," + KEY_ARRIVAL + " TEXT," + KEY_ALARMTIME
			+ " TEXT," + KEY_ALARMFREQ + " TEXT)";

	public AlarmHelper(Context context) {

		// TODO Auto-generated constructor stub
		super(context, dbName, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		// TODO Auto-generated method stub
		db.execSQL(MYFAVORITES_TABLE_CREATE);
		ContentValues cv = new ContentValues();
		cv.put(KEY_ID, 1);
		cv.put(KEY_TIME, "6:30");
		cv.put(KEY_ALARMTIME, "6:20");
		cv.put(KEY_DEPARTURE, "Carnegie Mellon University");
		cv.put(KEY_ARRIVAL, "Murray at Forbes");
		cv.put(KEY_ALARMFREQ, "Each Day");
		db.insert(MYFAVORITES_TABLE_NAME, KEY_ID, cv);
		cv.put(KEY_ID, 2);
		cv.put(KEY_TIME, "7:30");
		cv.put(KEY_ALARMTIME, "7:20");
		cv.put(KEY_DEPARTURE, "Murray at Forbes");
		cv.put(KEY_ARRIVAL, "Carnegie Mellon University");
		db.insert(MYFAVORITES_TABLE_NAME, KEY_ID, cv);
	

	}

	public Boolean insertAlarm(Alarm alarm) {

		Boolean ok = false;
		SQLiteDatabase db = this.getWritableDatabase();
		String[] params = new String[] { String.valueOf(alarm.getColTime()
				.toString()) };
		Cursor cur = db.rawQuery("SELECT * FROM " + MYFAVORITES_TABLE_NAME
				+ " WHERE " + KEY_TIME + "=?", params);
		int x = cur.getCount();
		if (x != 0)
			return ok;
		ContentValues cv = new ContentValues();
		cv.put(KEY_DEPARTURE, alarm.getColDep());
		cv.put(KEY_ARRIVAL, alarm.getColArr());
		cv.put(KEY_TIME, alarm.getColTime());
		cv.put(KEY_ALARMTIME, alarm.getColAlarmTime());
		cv.put(KEY_ALARMFREQ, alarm.getColAlarmFrequency());
		db.insert(MYFAVORITES_TABLE_NAME, KEY_DEPARTURE, cv);
		db.close();
		ok = true;
		return ok;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}



	public ArrayList<String> getAllAlarmTime() {
		SQLiteDatabase db = this.getWritableDatabase();
		ArrayList<String> al = new ArrayList<String>();
		// Cursor cur=
		// db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable,
		// new String [] {});
		Cursor cur = db.rawQuery("SELECT * FROM " + MYFAVORITES_TABLE_NAME,
				null);

		int size = cur.getCount();
		cur.moveToFirst();
		for (int i = 0; i < size; i++) {
			String name = cur.getString(cur.getColumnIndex(KEY_TIME));
			cur.moveToNext();

			al.add(name);
		}
		cur.close();
		return al;

	}

	public ArrayList<String> getAllAlarm() {
		SQLiteDatabase db = this.getWritableDatabase();
		ArrayList<String> al = new ArrayList<String>();
		// Cursor cur=
		// db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable,
		// new String [] {});
		Cursor cur = db.rawQuery("SELECT * FROM " + MYFAVORITES_TABLE_NAME,
				null);

		int size = cur.getCount();
		cur.moveToFirst();
		for (int i = 0; i < size; i++) {
			String time = cur.getString(cur.getColumnIndex(KEY_TIME));
			String dep = cur.getString(cur.getColumnIndex(KEY_DEPARTURE));
			String arr = cur.getString(cur.getColumnIndex(KEY_ARRIVAL));
			String alarmtime = cur.getString(cur.getColumnIndex(this.KEY_ALARMTIME));
			String alarmFrequency = cur.getString(cur.getColumnIndex(this.KEY_ALARMFREQ));
			cur.moveToNext();
			String name = "Time: " + time + "\n From " + dep + "\n To " + arr 
			+ "\nAlarm Time: " + alarmtime + "\n Alarm Frequency: " + alarmFrequency
				;

			al.add(name);
		}
		cur.close();
		return al;

	}

	public String getArrByTime(String time) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] params = new String[] { String.valueOf(time) };

		Cursor c = db.rawQuery("SELECT " + KEY_ARRIVAL + " FROM "
				+ MYFAVORITES_TABLE_NAME + " WHERE " + KEY_TIME + "=?", params);
		c.moveToFirst();
		int index = c.getColumnIndex(KEY_ARRIVAL);
		return c.getString(index);
	}

	public String getDepByTime(String time) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] params = new String[] { String.valueOf(time) };

		Cursor c = db.rawQuery("SELECT " + KEY_DEPARTURE + " FROM "
				+ MYFAVORITES_TABLE_NAME + " WHERE " + KEY_TIME + "=?", params);
		c.moveToFirst();
		int index = c.getColumnIndex(KEY_DEPARTURE);
		return c.getString(index);
	}

	public int UpdateAlarm(Alarm myfav) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(KEY_DEPARTURE, myfav.getColDep());
		cv.put(KEY_ARRIVAL, myfav.getColArr());
		return db.update(MYFAVORITES_TABLE_NAME, cv, KEY_TIME + "=?",
				new String[] { String.valueOf(myfav.getColTime()) });

	}

	public void DeleteAlarm(Alarm myfav) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(MYFAVORITES_TABLE_NAME, KEY_ID + "=?",
				new String[] { String.valueOf(myfav.getColID()) });
		db.close();

	}

	public Boolean DeleteAlarm(String name) {
		Boolean ok = false;
		SQLiteDatabase db = this.getWritableDatabase();
		String[] params = new String[] { String.valueOf(name.toString()) };
		Cursor cur = db.rawQuery("SELECT * FROM " + MYFAVORITES_TABLE_NAME
				+ " WHERE " + KEY_TIME + "=?", params);
		int x = cur.getCount();
		if (x != 0) {
			db.delete(MYFAVORITES_TABLE_NAME, KEY_TIME + "=?",
					new String[] { String.valueOf(name) });
			ok = true;
			db.close();
			return ok;

		} else
			return ok;

	}
}
